package com.amos.muexamclient.framework.service.impl;

import com.amos.muexamclient.framework.service.RedisService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * Copyright © 2018 五月工作室. All rights reserved.
 *
 * @Package com.amos.muexamclient.framework.service.impl
 * @ClassName RedisServiceImpl
 * @Description TODO
 * @Author Amos
 * @Modifier
 * @Date 2020/3/25 21:31
 * @Version 1.0
 **/
@Service
@Slf4j
public class RedisServiceImpl implements RedisService {
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public boolean save(String key, Object value, Integer expireTime, TimeUnit timeUnit) {
        try {
            redisTemplate.opsForValue().set(key, value, expireTime, timeUnit);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            log.info(e.getMessage());
            return false;
        }
    }

    @Override
    public boolean save(String key, Object value, Integer expireTime) {
        return this.save(key, value, expireTime, TimeUnit.SECONDS);
    }

    @Override
    public <T> T getKey(String key) {
        return (T) redisTemplate.opsForValue().get(key);
    }

    @Override
    public void delete(String key) {
        redisTemplate.delete(key);
    }
}
